#include "pathsubclass.h"
#include <QDebug>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsWidget>
pathSubClass::pathSubClass()
{
    this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    this->setPen(QPen(QBrush(Qt::black),1));
    setAcceptHoverEvents(true);
    initContextMenus();
}

pathSubClass::pathSubClass(int number)
{

    this->indexVal=number;
    this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
     this->setPen(QPen(QBrush(Qt::black),1));
    setAcceptHoverEvents(true);
    initContextMenus();
}

pathSubClass::pathSubClass(QPainterPath path, int number)
{

    this->setPath(path);
    this->indexVal=number;
    this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    this->setPen(QPen(QBrush(Qt::black),1));
    setAcceptHoverEvents(true);
    initContextMenus();
}

pathSubClass::pathSubClass(pathSubClass *myItem)
{
    Q_UNUSED(myItem)
}

pathSubClass::pathSubClass(QPainterPath path, int number, int itemType)
{

    this->setPath(path);
    this->indexVal=number;
    this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
    this->setPen(QPen(QBrush(Qt::black),1));
    this->itemtype=itemType;
    setAcceptHoverEvents(true);
    initContextMenus();
}

void pathSubClass::setPathToItem(QPainterPath path)
{
    this->setPath(path);
}

QPainterPath pathSubClass::getPathFromItem()
{
    return this->path();
}

void pathSubClass::addRectToMap(int count, rectSubClass *rItem)
{
    controlRects.insert(count,rItem);
}

rectSubClass *pathSubClass::getRectfromMap(int number)
{
    rectSubClass *tempRect= controlRects.value(number);
    return tempRect;
}

void pathSubClass::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    if(Qt::LeftButton == event->button())
    {
        qDebug()<<"in path class press event - emitting signal : "<<event->pos();
        qDebug()<<this->indexVal<<" type:"<<this->itemtype;
        int temp=this->indexVal;
        emit setCurrentItemIndex(temp);
    }
    event->accept();
}

void pathSubClass::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event)
}

void pathSubClass::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    qDebug()<<"in context menu event : pathsubclass";
    QMenu myMenu;
    myMenu.addAction(drawEndRect);
    myMenu.addAction(act_removeEndRect);
    if(this->itemtype==2 || this->itemtype==4)
    {
        myMenu.addAction(drawControlRect);
        myMenu.addAction(act_removeControlRect);
    }

    myMenu.exec(event->screenPos());
}

void pathSubClass::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    myPen=new QPen(this->pen());
    this->setPen(QPen(QBrush(Qt::red),2));
}

void pathSubClass::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    this->setPen(*myPen);
}

void pathSubClass::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    this->setPen(QPen(QBrush(Qt::red),2));
}

void pathSubClass::initContextMenus()
{

    drawEndRect=new QAction(tr("Draw End Rects"),this);
    connect(drawEndRect,SIGNAL(triggered()),this,SLOT(drawMyEndRects()));
    act_removeEndRect=new QAction(tr("Remove End Rects"),this);
    connect(act_removeEndRect,SIGNAL(triggered()),this,SLOT(removeMyEndRects()));
    joinTwoCurves=new QAction(tr("Join Us.."),this);
    drawControlRect=new QAction(tr("Draw Control Rects "),this);
    connect(drawControlRect,SIGNAL(triggered()),this,SLOT(drawMyControlRects()));
    act_removeControlRect=new QAction(tr("Remove Control Rects"),this);
    connect(act_removeControlRect,SIGNAL(triggered()),this,SLOT(removeMyControlRects()));

    //connect(joinTwoCurves,SIGNAL(triggered()),this,slot());
}

int pathSubClass::getIndexVal()
{
    return this->indexVal;
}

int pathSubClass::getItemType()
{
    return this->itemtype;
}

void pathSubClass::removeRect(int i)
{
    this->controlRects.remove(i);
}

QPointF pathSubClass::getControlPoint(int index)
{
    QPointF *tempPoint=new QPointF((controlPoints[index]->x()),(controlPoints[index]->y()));
    return *tempPoint;
}

void pathSubClass::setControlPoint(QPointF myPoint, int index)
{
    controlPoints[index]=new QPointF(myPoint);
}

QPointF pathSubClass::getTempPoint(int index)
{
    QPointF *tempPoint=new QPointF((temp_points[index]->x()),(temp_points[index]->y()));
    return *tempPoint;
}

void pathSubClass::setTempPoint(QPointF myPoint, int index)
{
    temp_points[index]=new QPointF(myPoint);
}

void pathSubClass::setMyBasePath(QPainterPath path)
{
    myBasePath=new QPainterPath(path);
}
void pathSubClass::setMyPath(QPainterPath *path)
{
    this->setPath(*path);
}

QPainterPath pathSubClass::getMyBasePath()
{
    return (*myBasePath);
}

int pathSubClass::controlRectsCount()
{
    int count=controlRects.count();
    return count;
}

void pathSubClass::drawMyEndRects()
{
    qDebug()<<":drawMyControlRects()";
    emit addRectsToScene(this->indexVal,this->itemtype);
}

void pathSubClass::removeMyEndRects()
{
    qDebug()<<":removeMyControlRects()";
    emit removeRectsFromScene(this->indexVal,this->itemtype);
}

void pathSubClass::drawMyControlRects()
{
    qDebug()<<"drawMyControlRects()";
    emit addControlRectsToScene(this->indexVal,this->itemtype);
}

void pathSubClass::removeMyControlRects()
{
    qDebug()<<"removeMyControlRects()";
    emit removeControlRectsToScene(this->indexVal,this->itemtype);
}
